package org.example.config;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

@Configuration
@ComponentScan(basePackages = "org.example")
@EnableAspectJAutoProxy
@PropertySource(value = "classpath:db.properties")
public class DataSourceConfig {
    @Value("${jdbc.driver}")
    private String dbDriver;
    @Value("${jdbc.url}")
    private String dbUrl;
    @Value("${jdbc.username}")
    private String dbUser;
    @Value("${jdbc.password}")
    private String dbPass;

    /**
     * 配了具备连接池的数据源对象
     */
    @Bean
    public DataSource dataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setDriverClassName(dbDriver);
        dataSource.setJdbcUrl(dbUrl);
        dataSource.setUsername(dbUser);
        dataSource.setPassword(dbPass);
        return dataSource;
    }

    @Bean
    public JdbcTemplate jdbcTemplate() {
        return new JdbcTemplate(dataSource());
    }
}
